package com.c.ctgproject.ui.collect

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.CarPartHandlerAdapter
import com.c.ctgproject.adapter.guang.PartsListBean
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityCarHandlerPartBinding
import com.c.ctgproject.entity.guang.CarHandlerOverEntity
import com.c.ctgproject.entity.guang.CarPartHandlerEntityV2
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.google.gson.Gson
import kotlinx.android.synthetic.main.item_car_part_handler_list.view.*
import kotlinx.android.synthetic.main.picker_image_preview_activity.*
import org.greenrobot.eventbus.EventBus

/**
 *作者:LiBW
 *创建日期: 2021/9/10
 *描述: 拆配件 页面 配件拆解 页面 报废车拆配件 页面(三元催化)
 */
class CarHandlerPartActivity : BaseActivityV2(), CarPartHandlerAdapter.CarPartHandlerAdapterListener {
    private val carDismantleId by lazy { intent.getIntExtra(MyConstants.ID, -1) }
    private val mBinding by lazy { ActivityCarHandlerPartBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root


    private val mList = ArrayList<CarPartHandlerEntityV2.DataBean>()
    private val mAdapter=CarPartHandlerAdapter(this, mList, this)
    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION, -1) }
    override fun event() {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.carCanHandlerPart(carDismantleId), MyObserverHandlerLoading(this, loading) {
            MyLog.d("车辆拆配件拆解清单:$it")
            val data = getGson().fromJson(it, CarPartHandlerEntityV2::class.java).data
            data?.let { list ->
                list.forEach {
                    if(it.carEngine.handlerNull().isEmpty()){
                        it.isEnabled=true
                    }
                }
                mList.addAll(list)
                mBinding.mRecyclerView.adapter = mAdapter
                mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
            }
        })
    }

    override fun click() {
        //拆解
        mBinding.mConfirmBTN.setOnClickListener {
            val map = HashMap<String, Any>()
            map["carDismantleId"] = carDismantleId
            val partsList = ArrayList<PartsListBean>()
            val carsList = ArrayList<CarPartHandlerEntityV2.DataBean.CarListBean>()
            var configFlag = true
            mList.forEach { child1 ->
                if (child1.carType == 1 || child1.carType == 2) {
                    MyLog.d(child1.carEngine == null)
                    val carBean = CarPartHandlerEntityV2.DataBean.CarListBean()
                    carBean.carId = child1.carId
                    carBean.carEngine = child1.carEngine
                    if (carBean.carEngine.handlerNull().isEmpty()) {
                        configFlag = false
                    }
                    carsList.add(carBean)
                }
                child1.partsList?.forEach { child2 ->
                    val bean = PartsListBean()

                    var str = "0"
                    child2.partsCount?.let { count ->
                        if (count.isNotEmpty()) str = count
                    }
                    bean.partsCount = str
                    bean.dismantleCarItemId = child2.dismantleCarItemId
                    bean.partsItemId = child2.partsItemId
                    bean.carId = child2.carId

                    bean.partsName = child2.partsName
                    bean.abbreviation = child2.abbreviation
                    partsList.add(bean)
                }

            }
            map["partsList"] = partsList
            map["carList"] = carsList
            MyLog.d(Gson().toJson(map))
            if (configFlag) {
                ApiMethods.apiSubscribe(ApiMethods.retrofit.carHandlerFinish(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                    showToast("拆解完毕")
                    EventBus.getDefault().post(PositionEvent(mListPosition))
                    finish()
                })
            } else {
                showToast("存在未填写的发动机号")
            }


        }
    }

    override fun onNumChange(str: String, parentPosition: Int, position: Int) {
        mList[parentPosition].partsList!![position].partsCount = str
    }

    override fun onCarEnergyTextChange(str: String, position: Int) {
        mList[position].carEngine = str
        //mAdapter.notifyDataSetChanged()
    }

}